<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API文档 - AegisX</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .main-container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .page-header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .page-header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }
        
        .page-header p {
            font-size: 1.2em;
            opacity: 0.9;
        }
        
        .api-nav-tabs {
            background: #f8f9fa;
            border-bottom: 1px solid #dee2e6;
        }
        
        .api-nav-tabs .nav-link {
            color: #6c757d;
            font-weight: 500;
            border: none;
            border-radius: 0;
            padding: 15px 20px;
        }
        
        .api-nav-tabs .nav-link.active {
            background: white;
            color: #007bff;
            border-bottom: 3px solid #007bff;
        }
        
        .api-nav-tabs .nav-link:hover {
            background: #e9ecef;
            color: #495057;
            border-color: transparent;
        }
        
        .api-content {
            padding: 30px;
        }
        
        .api-section {
            margin-bottom: 30px;
        }
        
        .api-card {
            border: 1px solid #e9ecef;
            border-radius: 10px;
            margin-bottom: 20px;
            overflow: hidden;
            transition: all 0.3s ease;
        }
        
        .api-card:hover {
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            transform: translateY(-2px);
        }
        
        .api-header {
            background: #f8f9fa;
            padding: 20px;
            border-bottom: 1px solid #e9ecef;
            cursor: pointer;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .api-title {
            font-size: 1.3em;
            font-weight: 600;
            color: #2c3e50;
        }
        
        .api-method {
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 0.8em;
            font-weight: bold;
            text-transform: uppercase;
        }
        
        .method-get {
            background: #d4edda;
            color: #155724;
        }
        
        .method-post {
            background: #d1ecf1;
            color: #0c5460;
        }
        
        .method-put {
            background: #fff3cd;
            color: #856404;
        }
        
        .method-delete {
            background: #f8d7da;
            color: #721c24;
        }
        
        .api-body {
            padding: 20px;
            display: none;
        }
        
        .api-body.show {
            display: block;
        }
        
        .api-description {
            color: #6c757d;
            margin-bottom: 20px;
            line-height: 1.6;
        }
        
        .api-details {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .detail-section {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
        }
        
        .detail-title {
            font-weight: 600;
            color: #495057;
            margin-bottom: 10px;
        }
        
        .parameter-list {
            list-style: none;
        }
        
        .parameter-item {
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }
        
        .parameter-item:last-child {
            border-bottom: none;
        }
        
        .parameter-name {
            font-weight: 500;
            color: #007bff;
        }
        
        .parameter-type {
            color: #6c757d;
            font-size: 0.9em;
        }
        
        .try-it-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
        }
        
        .input-group {
            margin-bottom: 15px;
        }
        
        .input-label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: #495057;
        }
        
        .input-field {
            width: 100%;
            padding: 10px;
            border: 1px solid #ced4da;
            border-radius: 5px;
            font-size: 14px;
        }
        
        .input-field:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(0,123,255,0.25);
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #007bff;
            color: white;
        }
        
        .btn-primary:hover {
            background: #0056b3;
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #545b62;
        }
        
        .response-section {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
            display: none;
        }
        
        .response-section.show {
            display: block;
        }
        
        .response-header {
            font-weight: 600;
            margin-bottom: 10px;
            color: #495057;
        }
        
        .response-body {
            background: #2d3748;
            color: #e2e8f0;
            padding: 15px;
            border-radius: 5px;
            font-family: 'Courier New', monospace;
            font-size: 13px;
            white-space: pre-wrap;
            max-height: 300px;
            overflow-y: auto;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #007bff;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .plugin-info {
            background: #e7f3ff;
            border: 1px solid #b3d9ff;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
        }
        
        .plugin-name {
            font-size: 1.2em;
            font-weight: 600;
            color: #0056b3;
            margin-bottom: 5px;
        }
        
        .plugin-version {
            color: #6c757d;
            font-size: 0.9em;
            margin-bottom: 10px;
        }
        
        .plugin-description {
            color: #495057;
            line-height: 1.5;
        }
        
        .search-box {
            width: 100%;
            padding: 12px 20px;
            border: 1px solid #ced4da;
            border-radius: 25px;
            font-size: 16px;
            margin-bottom: 20px;
        }
        
        .search-box:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(0,123,255,0.25);
        }
        
        .stats {
            display: flex;
            justify-content: space-around;
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 30px;
        }
        
        .stat-item {
            text-align: center;
        }
        
        .stat-number {
            font-size: 2em;
            font-weight: bold;
            color: #007bff;
        }
        
        .stat-label {
            color: #6c757d;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="page-header">
            <h1><i class="bi bi-rocket me-2"></i>AegisX API文档中心</h1>
            <p>动态插件接口文档与在线调用平台</p>
        </div>
        
        <ul class="nav nav-tabs api-nav-tabs" id="apiTabs" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="plugins-tab" data-bs-toggle="tab" data-bs-target="#plugins" type="button" role="tab">
                    <i class="bi bi-plug me-1"></i>插件API
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="system-tab" data-bs-toggle="tab" data-bs-target="#system" type="button" role="tab">
                    <i class="bi bi-gear me-1"></i>系统API
                </button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="overview-tab" data-bs-toggle="tab" data-bs-target="#overview" type="button" role="tab">
                    <i class="bi bi-bar-chart me-1"></i>概览
                </button>
            </li>
        </ul>
        
        <div class="tab-content api-content" id="apiTabContent">
            <!-- 概览标签页 -->
            <div class="tab-pane fade" id="overview" role="tabpanel">
                <div class="row mb-4">
                    <div class="col-md-4">
                        <div class="card text-center">
                            <div class="card-body">
                                <div class="stat-number" id="plugin-count">0</div>
                                <div class="stat-label">插件数量</div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card text-center">
                            <div class="card-body">
                                <div class="stat-number" id="system-api-count">0</div>
                                <div class="stat-label">系统API</div>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card text-center">
                            <div class="card-body">
                                <div class="stat-number" id="total-api-count">0</div>
                                <div class="stat-label">总API数</div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h2><i class="bi bi-list-check me-2"></i>快速开始</h2>
                    </div>
                    <div class="card-body">
                        <h3><i class="bi bi-bullseye me-2"></i>使用说明</h3>
                        <ul class="list-unstyled ms-3">
                            <li class="mb-2"><i class="bi bi-plug text-primary me-2"></i><strong>插件API</strong>：查看和调用已加载的插件接口</li>
                            <li class="mb-2"><i class="bi bi-gear text-primary me-2"></i><strong>系统API</strong>：框架提供的核心管理接口</li>
                            <li class="mb-2"><i class="bi bi-play-circle text-primary me-2"></i><strong>在线调用</strong>：直接在页面中测试API接口</li>
                            <li class="mb-2"><i class="bi bi-arrow-clockwise text-primary me-2"></i><strong>实时更新</strong>：插件加载后自动更新文档</li>
                        </ul>
                        
                        <h3 class="mt-4"><i class="bi bi-tools me-2"></i>API调用示例</h3>
                        <div class="response-body mt-3">
// 调用插件API
fetch('/api-docs/call/插件名称', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        input: '输入参数'
    })
})
.then(response => response.json())
.then(data => console.log(data));
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 插件API标签页 -->
            <div class="tab-pane fade show active" id="plugins" role="tabpanel">
                <div class="mb-3">
                    <input type="text" class="form-control search-box" placeholder="🔍 搜索插件..." onkeyup="filterPlugins(this.value)">
                </div>
                <div id="plugin-apis"></div>
            </div>
            
            <!-- 系统API标签页 -->
            <div class="tab-pane fade" id="system" role="tabpanel">
                <div class="mb-3">
                    <input type="text" class="form-control search-box" placeholder="🔍 搜索系统API..." onkeyup="filterSystemApis(this.value)">
                </div>
                <div id="system-apis"></div>
            </div>
        </div>
    </div>
    
    <script>
        let apiData = null;
        
        // 页面加载时获取API数据
        document.addEventListener('DOMContentLoaded', function() {
            loadApiData();
        });
        
        // 加载API数据
        async function loadApiData() {
            try {
                console.log('开始加载API数据...');

                const response = await fetch('/api-docs/data');
                const result = await response.json();
                console.log('API响应数据:', result);
                
                if (result.code === 200) {
                    apiData = result.data;
                    console.log('解析的API数据:', apiData);
                    console.log('插件数量:', apiData.plugins ? apiData.plugins.length : 0);
                    console.log('系统API数量:', apiData.systemApis ? apiData.systemApis.length : 0);
                    

                    
                    renderPluginApis(apiData.plugins);
                    renderSystemApis(apiData.systemApis);
                    updateStats();
                } else {
                    console.error('加载API数据失败:', result.message || 'Unknown error');
                    document.getElementById('plugin-apis').innerHTML = '<div class="api-card"><div class="api-body show"><div class="api-description"><h3>❌ 加载失败</h3><p>API数据加载失败: ' + (result.message || 'Unknown error') + '</p></div></div></div>';
                }
            } catch (error) {
                console.error('请求失败:', error);
                document.getElementById('plugin-apis').innerHTML = '<div class="api-card"><div class="api-body show"><div class="api-description"><h3>❌ 网络错误</h3><p>无法连接到服务器: ' + error.message + '</p></div></div></div>';
            }
        }
        
        // 渲染插件API
        function renderPluginApis(plugins) {
            console.log('开始渲染插件API, 插件数据:', plugins);
            const container = document.getElementById('plugin-apis');
            console.log('找到容器元素:', container);
            
            if (!plugins || plugins.length === 0) {
                console.log('没有插件数据，显示暂无插件提示');
                container.innerHTML = `
                    <div class="api-card">
                        <div class="api-body show">
                            <div class="api-description">
                                <h3>📦 暂无插件</h3>
                                <p>系统中还没有加载任何插件。请先加载插件后再查看API文档。</p>
                            </div>
                        </div>
                    </div>
                `;
                return;
            }
            
            console.log('开始生成插件HTML，插件数量:', plugins.length);
            
            container.innerHTML = plugins.map(plugin => `
                <div class="api-card plugin-card" data-plugin="${plugin.name.toLowerCase()}">
                    <div class="plugin-info">
                        <div class="plugin-name">${plugin.name}</div>
                        <div class="plugin-version">版本: ${plugin.version}</div>
                        <div class="plugin-description">${plugin.description}</div>
                    </div>
                    
                    <div class="api-header" onclick="toggleApiBody(this)">
                        <div class="api-title">🔌 ${plugin.name} API</div>
                        <span class="api-method method-post">POST</span>
                    </div>
                    
                    <div class="api-body">
                        <div class="api-description">
                            <strong>端点:</strong> ${plugin.api.endpoint}<br>
                            <strong>方法:</strong> ${plugin.api.method}<br>
                            <strong>内容类型:</strong> ${plugin.api.contentType}
                        </div>
                        
                        <div class="api-details">
                            <div class="detail-section">
                                <div class="detail-title">📋 插件方法</div>
                                <ul class="parameter-list">
                                    ${plugin.methods.map(method => `
                                        <li class="parameter-item">
                                            <span class="parameter-name">${method.name}</span>
                                            <span class="parameter-type">(${method.parameters.map(p => p.type).join(', ')}) → ${method.returnType}</span>
                                        </li>
                                    `).join('')}
                                </ul>
                            </div>
                            
                            <div class="detail-section">
                                <div class="detail-title">📝 请求格式</div>
                                <div class="response-body">
{
  "input": "输入参数"
}
                                </div>
                            </div>
                        </div>
                        
                        <div class="try-it-section">
                            <h4>🧪 在线测试</h4>
                            <div class="input-group">
                                <label class="input-label">输入参数:</label>
                                <textarea class="input-field" rows="3" placeholder='输入JSON格式的参数，例如: "Hello World"' id="input-${plugin.name}"></textarea>
                            </div>
                            <button class="btn btn-primary" onclick="callPluginApi('${plugin.name}')">
                                <span class="btn-text">🚀 调用API</span>
                                <span class="loading" style="display: none;"></span>
                            </button>
                            <button class="btn btn-secondary" onclick="clearResponse('${plugin.name}')">🗑️ 清空</button>
                            
                            <div class="response-section" id="response-${plugin.name}">
                                <div class="response-header">📤 响应结果:</div>
                                <div class="response-body" id="response-body-${plugin.name}"></div>
                            </div>
                        </div>
                    </div>
                </div>
            `).join('');
        }
        
        // 渲染系统API
        function renderSystemApis(systemApis) {
            const container = document.getElementById('system-apis');
            
            container.innerHTML = systemApis.map(api => `
                <div class="api-card system-api-card" data-api="${api.name.toLowerCase()}">
                    <div class="api-header" onclick="toggleApiBody(this)">
                        <div class="api-title">⚙️ ${api.name}</div>
                        <span class="api-method method-${api.method.toLowerCase()}">${api.method}</span>
                    </div>
                    
                    <div class="api-body">
                        <div class="api-description">
                            ${api.description}
                        </div>
                        
                        <div class="api-details">
                            <div class="detail-section">
                                <div class="detail-title">🔗 API信息</div>
                                <div><strong>路径:</strong> ${api.path}</div>
                                <div><strong>方法:</strong> ${api.method}</div>
                            </div>
                            
                            <div class="detail-section">
                                <div class="detail-title">📖 使用示例</div>
                                <div class="response-body">
fetch('${api.path}', {
    method: '${api.method}'
})
.then(response => response.json())
.then(data => console.log(data));
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            `).join('');
        }
        
        // Bootstrap会自动处理tab切换，不需要自定义switchTab函数
        
        // 切换API详情显示
        function toggleApiBody(header) {
            const body = header.nextElementSibling;
            body.classList.toggle('show');
        }
        
        // 调用插件API
        async function callPluginApi(pluginName) {
            const inputElement = document.getElementById(`input-${pluginName}`);
            const responseElement = document.getElementById(`response-${pluginName}`);
            const responseBodyElement = document.getElementById(`response-body-${pluginName}`);
            const button = event.target;
            const btnText = button.querySelector('.btn-text');
            const loading = button.querySelector('.loading');
            
            // 显示加载状态
            button.disabled = true;
            if (btnText) {
                btnText.style.display = 'none';
            }
            if (loading) {
                loading.style.display = 'inline-block';
            }
            
            try {
                let input = inputElement.value.trim();
                let requestBody = {};
                
                if (input) {
                    try {
                        // 尝试解析为JSON
                        input = JSON.parse(input);
                    } catch (e) {
                        // 如果不是JSON，直接使用字符串
                    }
                    requestBody.input = input;
                }
                
                const response = await fetch(`/api-docs/call/${pluginName}`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestBody)
                });
                
                const result = await response.json();
                
                // 显示响应
                responseBodyElement.textContent = JSON.stringify(result, null, 2);
                responseElement.classList.add('show');
                
            } catch (error) {
                responseBodyElement.textContent = `请求失败: ${error.message}`;
                responseElement.classList.add('show');
            } finally {
                // 恢复按钮状态
                button.disabled = false;
                if (btnText) {
                    btnText.style.display = 'inline';
                }
                if (loading) {
                    loading.style.display = 'none';
                }
            }
        }
        
        // 清空响应
        function clearResponse(pluginName) {
            const inputElement = document.getElementById(`input-${pluginName}`);
            const responseElement = document.getElementById(`response-${pluginName}`);
            
            inputElement.value = '';
            responseElement.classList.remove('show');
        }
        
        // 过滤插件
        function filterPlugins(searchTerm) {
            const cards = document.querySelectorAll('.plugin-card');
            
            cards.forEach(card => {
                const pluginName = card.dataset.plugin;
                if (pluginName.includes(searchTerm.toLowerCase())) {
                    card.style.display = 'block';
                } else {
                    card.style.display = 'none';
                }
            });
        }
        
        // 过滤系统API
        function filterSystemApis(searchTerm) {
            const cards = document.querySelectorAll('.system-api-card');
            
            cards.forEach(card => {
                const apiName = card.dataset.api;
                if (apiName.includes(searchTerm.toLowerCase())) {
                    card.style.display = 'block';
                } else {
                    card.style.display = 'none';
                }
            });
        }
        
        // 更新统计信息
        function updateStats() {
            if (apiData) {
                document.getElementById('plugin-count').textContent = apiData.plugins.length;
                document.getElementById('system-api-count').textContent = apiData.systemApis.length;
                document.getElementById('total-api-count').textContent = apiData.plugins.length + apiData.systemApis.length;
            }
        }
    </script>
    
    <!-- Bootstrap JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>